package com.ylyq.ylxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.entity.YlyqPlanStopStart;
import com.ylyq.ylxx.entity.YlyqRefusePlan;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.*;
import com.ylyq.ylxx.model.request.*;
import com.ylyq.ylxx.service.IOceanegineCreativeService;
import com.ylyq.ylxx.service.IOceaneginePlanService;
import com.ylyq.ylxx.service.IYlyqPlanStopStartService;
import com.ylyq.ylxx.service.IYlyqPlanToolService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName YlyqPlanToolServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/8/31 14:10
 * @Version 1.0
 */
@Service
public class YlyqPlanToolServiceImpl implements IYlyqPlanToolService {

    @Autowired
    private IOceaneginePlanService iOceaneginePlanService;

    @Autowired
    private IOceanegineCreativeService iOceanegineCreativeService;

    @Autowired
    private IYlyqPlanStopStartService iYlyqPlanStopStartService;

    @Override
    public ResponseData copyPlan(CopyPlanRequest copyPlanRequest) {
//获取计划详情
        GetPlanInfoRequest getPlanInfoRequest = new GetPlanInfoRequest();
        getPlanInfoRequest.setAdvertiser_id(copyPlanRequest.getAdvertiser_id());
        getPlanInfoRequest.setPage_size(copyPlanRequest.getPlanIds().length);
        GetPlanInfoRequest.FilteringGetPlanInfoRequest filteringGetPlanInfoRequest = new GetPlanInfoRequest.FilteringGetPlanInfoRequest();
        filteringGetPlanInfoRequest.setIds(copyPlanRequest.getPlanIds());
        getPlanInfoRequest.setFiltering(filteringGetPlanInfoRequest);
        OceanenginePlanInfo planInfo = iOceaneginePlanService.getPlan(getPlanInfoRequest);
        if (null == planInfo) {
            return new ResponseData(ResponseData.Result.FAIL);
        }
        if (planInfo.getCode() == 40002) {//这种情况是出现在用户在巨量上手动删除的,要在易量系统中也删除
            return new ResponseData(planInfo.getCode(), "选中的计划被用户在巨量手动删除了");
        }
        List<OceanenginePlanInfo.DataBean.ListBean> planInfoList = planInfo.getData().getList();
        if (null == planInfoList || planInfoList.isEmpty())
            return new ResponseData(ResponseData.Result.FAIL);

        List<YlyqPlanStopStart> ylyqPlanStopStartList=new ArrayList<>();
        for (OceanenginePlanInfo.DataBean.ListBean planDetail : planInfoList) {
            for (int i = 0; i < copyPlanRequest.getNums(); i++) {
                CreatePlanInfoRequest createPlanInfoRequest = new CreatePlanInfoRequest();
                BeanUtils.copyProperties(planDetail, createPlanInfoRequest);
                createPlanInfoRequest.setName(createPlanInfoRequest.getName() + "_re"+i);
                OceanengineCreatePlanResult planResult = iOceaneginePlanService.createPlan(createPlanInfoRequest);
                if (planResult.getCode() == AccountConstant.SUCCESS) {//新建计划。不管成功与否，关联计划得创意都要删除
                    //新建计划关联计划的创意
                    CreativeDetailRequest creativeDetailRequest = new CreativeDetailRequest();
                    creativeDetailRequest.setAd_id(planDetail.getId());
                    creativeDetailRequest.setAdvertiser_id(copyPlanRequest.getAdvertiser_id());
                    //取当前的创意详情
                    OceanengineCreativeDetail oceanengineCreativeDetail = iOceanegineCreativeService.creativeDetail(creativeDetailRequest);
                    if (oceanengineCreativeDetail.getCode() == AccountConstant.SUCCESS) {
                        //新建创意
                        CreateCreativeRequest createCreativeRequest = new CreateCreativeRequest();
                        BeanUtils.copyProperties(oceanengineCreativeDetail.getData(), createCreativeRequest);
                        createCreativeRequest.setAd_id(planResult.getData().getAd_id());
                        OceanengineCreateCreativeResult creative = iOceanegineCreativeService.createCreative(createCreativeRequest);
//                                    creative.getCode();
                    }
                    //易量新建
                    YlyqPlanStopStart ylyqPlanStopStart = new YlyqPlanStopStart();
                    ylyqPlanStopStart.setPlanId(planResult.getData().getAd_id());
                    ylyqPlanStopStart.setAdvertiserId(copyPlanRequest.getAdvertiser_id());
                    ylyqPlanStopStart.setGroupId(planDetail.getCampaign_id());
                    ylyqPlanStopStart.setUserId(FilterContextHandler.getUserID());
                    ylyqPlanStopStartList.add(ylyqPlanStopStart);
                }
            }
        }

        //易量新建
        if (!ylyqPlanStopStartList.isEmpty())
            iYlyqPlanStopStartService.insertBatch(ylyqPlanStopStartList);

        return new ResponseData(ResponseData.Result.SUCCESS);
    }
}
